<?php
namespace action;
use GameRedis\RedisPackage;

class User{

    /**
     * 用户登陆
     * @param 前端传递用户登陆信息 $message
     */
    public function login($message){
      
       /*  $str = '{"ws":"10","loginType":"1","name":"dsfsdf","url":"sdfdsfdsf","openid":"111111111111"}';
        */ 
        $us = array();
        $userModel = db("users");
        $message = $message['body'];
        
        if(isset($message['loginType'])&&$message['loginType']==2){
            if(!isset($message['pwd'])||!isset($message['name'])){
                $us['ok'] = "2";
                $us['content'] = "必要数据不能有空";
                return $us;
            }
            $pwd = $message['pwd'];
            $us = $userModel->field("mobile,id,user_money,url,nickname,daili")->where("mobile='{$message['name']}' and pwd='{$pwd}'")->find();
            if($us){
                $us['ok'] = "1";
            }else{
                $us['ok'] = "2";
                $us['content'] = "账户或密码错误";
            }
        }else if($message['loginType']==1){
            $id = 0;
            $status = 0;
            if(!isset($message['name'])||!isset($message['url'])||!isset($message['openid'])){
                $us['ok'] = "2";
                $us['content'] = "必要数据不能有空";
                return $us;
            }
            $da['nickname'] = $message['name'];
            $da['url'] = $message['url'];
            
            $u = $userModel->where("openid='{$message['openid']}'")->find();
            if($u){
                $userModel->where("openid='{$message['openid']}'")->update($da);
                $status = 2;
                $id = $u['id'];
            }else{
                $da['openid'] = $message['openid'];
                $id = $userModel->insertGetId($da);
                $status= 1;//status为一的时候是第一次登陆需要绑定手机账户
            }
            //微信登陆
            $us = $userModel->where("id={$id}")->field("mobile,id,user_money,url,nickname,daili")->find();
            if($us){
                $us['ok'] = "1";
                
                $us['status'] = $status;
            }else{
                $us['ok'] = "2";
                $us['content'] = "账户或密码错误";
            }
        }else{
                $us['ok'] = "2";
                $us['content'] = "登陆类型错误";
        }
        if(isset($us['id'])){
            if($us['daili']==1){
                $id = $us['id'];
                if(strlen($id)<4){
                    $id = "0000".$id;
                    $id = substr($id, -4);
                }
                $us['yqm'] = $id;
            }
            $us['ws'] = 10;
            
        }
        $us['dl'] = "您不是代理,想成为代理赚取佣金请联系客服！";
        return $us;
    }
    /**
     * 用户绑定手机号
     * @param unknown $message
     * @return string[]
     */
    public function wacht($message){
        
        $userModel = db("users");
        $pwd = md5("myfirstapp".$message['passWord']);
        
        $num = $message['num'];//获取邀请码
        $num = ltrim($num,0);
        $saveArray = array("mobile"=>$message['mobile'],"pwd"=>$pwd);
        if($num>0){
            $parent = $userModel->where("id={$num}")->find();
            if($parent){
                $saveArray['first_parent'] = $num;
                $saveArray['second_parent'] = $parent['first_parent'];
                $saveArray['three_parent'] = $parent['second_parent'];
            }
        }
        $a = $userModel->where("id={$message['id']}")->update($saveArray);
        $data = array();
        if($a){
            $data['ok'] = "success";
        }else{
             $data['ok'] = "fail";
             $data['content'] = "绑定失败";
        }
        return $data;
    }

    
      
    
    public function reg($message){
        $userModel = db("users");
        $mobile = $message['mobile'];
        $nickname = $message['nickname'];
        $pwd = $message['pwd'];
       /*  $yqm = $message['yqm']; */
        $wenti = $message['wenti'];
      /*   $num = ltrim($yqm,0); */
        
        
        $aaa = $userModel->where("mobile={$mobile}")->count();
        if($aaa>0){
            return array("ok"=>2,"content"=>"已有次账户");
        }
        
        /* if(!$num){
            return array("ok"=>2,"content"=>"邀请码不能为空");
        } */
       /*  if($num&&$num!="undefined"){
            if($num!=1){
                $parent = $userModel->where("id={$num} and daili=1")->find();
                if($parent){
                    $saveArray['first_parent'] = $num;
                    $saveArray['second_parent'] = $parent['first_parent'];
                    $saveArray['three_parent'] = $parent['second_parent'];
                    $saveArray['user_money'] = 200;
                }else{
                    return array("ok"=>2,"content"=>"邀请码错误");
                }
            } 
        } */
        $saveArray['mobile'] = $mobile;
        $saveArray['pwd'] = $pwd;
        $saveArray['reg_time'] = time();
        $saveArray['state'] = 1;
        $saveArray['nickname'] = $nickname;
        $saveArray['wenti'] = $wenti;
        $saveArray['user_money'] = 1000;
        $saveArray['is_create'] = 1;
        $userModel->insert($saveArray);
        return array("ok"=>1,"ws"=>14);
            
    }
    
    public function watchLogin($message,$code){
        $appid ="wx2d02f9a9a5390f3c";
        $key = "454ecee469749ad8c535b6c843bc328a";
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$key."&code=".$code."&grant_type=authorization_code";
        $content = $this->http_get($url);
        if(!$content){
            return array("ok"=>2,"content"=>"openid失败");
        }
        
        $json = json_decode($content,true);
        $openid = $json['openid'];
        $access_token = $json['access_token'];    
        $surl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
        $request = $this->http_get($surl);
        
        if(!$request){
            return array("ok"=>2,"content"=>"nickname fail");
        }
        
        $user = json_decode($request,true);
        
        
   
        
        
        
        
        
        $userModel = db("users");
        $da['nickname'] = $user['nickname'];
        $da['url'] = $user['headimgurl'];
        
        $u = $userModel->where("openid='{$user['openid']}'")->find();
        if($u){
            $userModel->where("openid='{$user['openid']}'")->update($da);
            $status = 2;
            $id = $u['id'];
        }else{
            $da['openid'] = $user['openid'];
            $id = $userModel->insertGetId($da);
            $status= 1;//status为一的时候是第一次登陆需要绑定手机账户
        }
        //微信登陆
        $us = $userModel->where("id={$id}")->field("mobile,id,user_money,url,nickname")->find();
        if($us){
            $us['ok'] = "1";
            $us['ws'] = 15;
            $us['status'] = $status;
        }else{
            $us['ok'] = "2";
            $us['content'] = "账户或密码错误";
        }
        return $us;
        
    }
    
    
    
    
    public function http_get($url){
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }
    }
}